package com.mapler.autoresponder.ui;

import com.mapler.ngfx.ui.*;
import com.mapler.utility.HttpHelper;
import com.mapler.utility.IConstant;
import java.awt.Color;
import java.net.URL;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

/**
 *
 * @author none
 */
public class ViewAutoResponderAccUI extends javax.swing.JPanel {

    private javax.swing.JButton jButtonDelete;
    private javax.swing.JTextField emailUI;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableAccess;
    private javax.swing.JLabel message;
    private javax.swing.JTextField recordIdUI;
    
    private String[] COLUMNS = {"ID", "Username", "Password","Serial","Proxy"};
    private DefaultTableModel model = new DefaultTableModel(COLUMNS, 0);
    
    private String username;
    private String password;
    private String machine;
    
    private static Logger log = Logger.getLogger(ViewNGFXAccUI.class);
    public ViewAutoResponderAccUI() {
        initComponents();
    }
    
    public ViewAutoResponderAccUI(String username, String password,String machine) {
         this.username = username;
         this.password = password;
         this.machine = machine;
         initComponents();
         this.getAccounts();
    }
    
    private void initComponents() {
        message = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        recordIdUI = new javax.swing.JTextField();
        emailUI = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButtonDelete = new javax.swing.JButton();
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionDeleteHandler(evt);
            }
        });
        jButtonDelete.setVisible(false);

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableAccess = new javax.swing.JTable(model);
        jTableAccess.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent e) {
                int row = jTableAccess.rowAtPoint(e.getPoint());
                int col = jTableAccess.columnAtPoint(e.getPoint());
                recordIdUI.setText(jTableAccess.getValueAt(row, 0).toString());
                emailUI.setText(jTableAccess.getValueAt(row, 1).toString());
                //System.out.println(row + ":" + col + ": Value in the cell clicked:" + jTableAccount.getValueAt(row, col).toString());
            }
        });

        jLabel1.setText("ID");
        jLabel2.setText("Email");
        jButtonDelete.setText("Delete");
        jScrollPane1.setViewportView(jTableAccess);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 507, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jButtonDelete)
                .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(recordIdUI, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(83, 83, 83)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(emailUI, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 556, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(70, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(recordIdUI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(emailUI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54)));
    }
    
    private void actionDeleteHandler(java.awt.event.ActionEvent evt) {
        try {
            StringBuilder request = new StringBuilder();
            request.append("<Request>");
            request.append("<id>").append(this.recordIdUI.getText()).append("</id>");
            request.append("<username>").append(this.username).append("</username>");
            request.append("<password>").append(this.password).append("</password>");            
            request.append("</Request>");

            String uri = "http://" + IConstant.HOSTNAME + "/index.php?r=autoresponderAccount/deleteAccount&request=" + request.toString();
            //System.out.println("::::::::::::" + uri);

            String response = HttpHelper.post(new URL(uri), "");

            Document document = DocumentHelper.parseText(response);
            String errorCode = document.valueOf("Response/errorcode");
            String msg = document.valueOf("Response/message");
            if (errorCode.equalsIgnoreCase("000")) {
                this.message.setBackground(Color.red);
            } else {
                this.getAccounts();
                this.message.setBackground(Color.black);
            }
            this.message.setText(msg);
        } catch (Exception ex) {
            log.debug("actionDeleteHandler:: Error "+ex);
            this.message.setText("Error "+ex.getMessage());
        }
    }
    
    private void clearModel() {
        for (int i = model.getRowCount() - 1; i >= 0; i--) {
            model.removeRow(i);
        }
        jTableAccess.repaint();            
    }
    public void getAccounts() {
        try {
            this.clearModel();
            String uri = "http://" + IConstant.HOSTNAME + "/index.php?r=autoresponderAccount/getAccounts&username="
                    + this.username + "&password=" + this.password;
            String response = HttpHelper.post(new URL(uri), "");
            Document document = DocumentHelper.parseText(response);
            String errorCode = document.valueOf("Response/errorcode");
            if (!errorCode.equalsIgnoreCase("000")) {
                List<Node> list = document.selectNodes("/Response/accounts/account");
                for (Node node : list) {
                    Object[] row = new Object[5];
                    row[0] = node.valueOf("id");
                    row[1] = node.valueOf("username");
                    row[2] = node.valueOf("password");
                    row[3] = "";
                    row[4] = "";
                    model.addRow(row);
                }
            } else {
                String message = document.valueOf("Response/message");
                this.message.setText(message);
            }
        } catch (Throwable ex) {
            this.message.setText("Error "+ex.getMessage());
            log.debug("Error "+ex);
            ex.printStackTrace();
        }
    }
}
